﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Threading;
using System.Collections.Generic;

/// <summary>
/// Summary description for SendEmailNotification
/// </summary>
namespace LibraryWinService
{
    public class SendEmailNotification
    {
        public void sendEmailNotificationofReservedBookAvailable()
        {
            DatabaseAccess accessor = new DatabaseAccess();
            DataSet ds = accessor.getReservedBooksToNotify();

            EmailService.EmailService mailer = new EmailService.EmailService();

            Dictionary<String, List<ReservedBook>> emails = new Dictionary<String, List<ReservedBook>>();

            foreach (DataRow theRow in ds.Tables[0].Rows)
            {
                String Email = theRow["Email"].ToString().Trim();
                String Title = theRow["Title"].ToString().Trim();
                String Author = theRow["Author"].ToString().Trim();
                String Status = theRow["ReserveStatus"].ToString().Trim();

                if (emails.ContainsKey(Email))
                {
                    emails[Email].Add(new ReservedBook(Title, Author, Status));
                }
                else
                {
                    emails.Add(Email, new List<ReservedBook>());
                    emails[Email].Add(new ReservedBook(Title, Author, Status));
                }
            }
            foreach (KeyValuePair<String, List<ReservedBook>> pair in emails)
            {
                String Email = pair.Key;
                List<ReservedBook> Books = pair.Value;

                String Subject = "Reserved Book Available";
                String Body = "<h4>Reserved Book Available</h4>";
                    Body += "<p>This is an automated message from the BPI Library System.</p>";
                    Body += "<p>The following book(s) that you reserved is now ready to be picked up: ";
                    Body += "<br><table>";
                    Body += "<tr><td>Title</td><td>Author</td><td>Status</td></tr>";
                foreach (ReservedBook book in Books)
                {
                    String Title = book.Title;
                    String Author = book.Author;
                    String Status = book.Status;
                    
                    Body += "<tr><td>" + Title + "</td><td>" + Author + "</td><td>" + Status + "</td></tr>";                    
                }
                Body += "</table></p>";
                mailer.sendEmail(Email, Subject, Body);
            }
        }

        public void sendEmailReminderofBooksDueSoon() 
        {
            DatabaseAccess accessor = new DatabaseAccess();
            DataSet ds = accessor.getBooksDueSoonToNotify();

            EmailService.EmailService mailer = new EmailService.EmailService();

            Dictionary<String, List<OverdueBook>> emails = new Dictionary<String, List<OverdueBook>>();

            foreach (DataRow theRow in ds.Tables[0].Rows)
            {
                String Email = theRow["Email"].ToString().Trim();
                String Title = theRow["Title"].ToString().Trim();
                String Author = theRow["Author"].ToString().Trim();
                String CheckedoutDate = theRow["DateCheckedOut"].ToString().Trim();
                String DueDate = theRow["DueDate"].ToString().Trim();

                if (emails.ContainsKey(Email))
                {
                    emails[Email].Add(new OverdueBook(Title, Author, CheckedoutDate, DueDate));
                }
                else
                {
                    emails.Add(Email, new List<OverdueBook>());
                    emails[Email].Add(new OverdueBook(Title, Author, CheckedoutDate, DueDate));
                }
            }
            foreach (KeyValuePair<String, List<OverdueBook>> pair in emails)
            {
                String Email = pair.Key;
                List<OverdueBook> Books = pair.Value;

                String Subject = "Book Due Soon";
                String Body = "<h4>Book Due Soon</h4>";
                Body += "<p>This is an automated message from the BPI Library System.</p>";
                Body += "<p>The following book(s) that you have checked out are due soon and should be returned or renewed as soon as possible: ";
                Body += "<br><table>";
                Body += "<tr><td>Title</td><td>Author</td><td>Date Checked Out</td><td>Due Date</td></tr>";
                foreach (OverdueBook book in Books)
                {
                    String Title = book.Title;
                    String Author = book.Author;
                    String CheckoutDate = book.CheckoutDate;
                    String DueDate = book.DueDate;

                    Body += "<tr><td>" + Title + "</td><td>" + Author + "</td><td>" + CheckoutDate + "</td><td>" + DueDate + "</td></tr>";
                }
                Body += "</table></p>";
                mailer.sendEmail(Email, Subject, Body);
            }                    
        }

        public void sendEmailNotificationofBooksOverdue() 
        {
            DatabaseAccess accessor = new DatabaseAccess();
            DataSet ds = accessor.getOverdueBooksToNotify();

            EmailService.EmailService mailer = new EmailService.EmailService();

            Dictionary<String, List<OverdueBook>> emails = new Dictionary<String, List<OverdueBook>>();

            foreach (DataRow theRow in ds.Tables[0].Rows)
            {
                String Email = theRow["Email"].ToString().Trim();
                String Title = theRow["Title"].ToString().Trim();
                String Author = theRow["Author"].ToString().Trim();
                String CheckedoutDate = theRow["DateCheckedOut"].ToString().Trim();
                String DueDate = theRow["DueDate"].ToString().Trim();

                if (emails.ContainsKey(Email))
                {
                    emails[Email].Add(new OverdueBook(Title, Author, CheckedoutDate,DueDate));
                }
                else
                {
                    emails.Add(Email, new List<OverdueBook>());
                    emails[Email].Add(new OverdueBook(Title, Author, CheckedoutDate, DueDate));
                }
            }
            foreach (KeyValuePair<String, List<OverdueBook>> pair in emails)
            {
                String Email = pair.Key;
                List<OverdueBook> Books = pair.Value;

                String Subject = "Overdue Book";
                String Body = "<h4>Overdue Book</h4>";
                Body += "<p>This is an automated message from the BPI Library System.</p>";
                Body += "<p>The following book(s) that you have checked out are overdue and should be returned or renewed as soon as possible: ";
                Body += "<br><table>";
                Body += "<tr><td>Title</td><td>Author</td><td>Date Checked Out</td><td>Due Date</td></tr>";
                foreach (OverdueBook book in Books)
                {
                    String Title = book.Title;
                    String Author = book.Author;
                    String CheckoutDate = book.CheckoutDate;
                    String DueDate = book.DueDate;

                    Body += "<tr><td>" + Title + "</td><td>" + Author + "</td><td>" + CheckoutDate + "</td><td>" + DueDate + "</td></tr>";
                }
                Body += "</table></p>";
                mailer.sendEmail(Email, Subject, Body);
            }
        }
    }
    // The delegate must have the same signature as the method
    // you want to call asynchronously.
    public delegate bool AsyncReserveNote();
}